package 双指针.复写零;

import java.util.Arrays;

public class ReZero {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {1,0,2,3,0,4,5,0};
        so.duplicateZeros(arr);
        System.out.println(Arrays.toString(arr));
    }
}
class Solution {
    public void duplicateZeros(int[] arr) {
        int cur = -1,dest = -1;
        while(dest < arr.length-1){
            cur++;
            if(arr[cur]!=0){
                dest++;
            }else{
                dest+=2;
            }
        }
        if(dest == arr.length){
            arr[dest-1] = 0;
            dest -= 2;
            cur--;
        }
//        System.out.println(cur);
//        System.out.println(dest);
        while(cur>=0){
            if(arr[cur]!=0){
                arr[dest--]=arr[cur--];
            }else{
                arr[dest--]=0;
                arr[dest--]=0;
                cur--;
            }
        }
    }
}